package com.lingyun.ucm.web.mvc.service.impl.configfile.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;

import org.I0Itec.zkclient.ZkClient;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lingyun.ucm.common.exception.UtilException;
import com.lingyun.ucm.common.pojo.bean.ConfigStoreValue;
import com.lingyun.ucm.common.util.FastJSONUtil;
import com.lingyun.ucm.common.zk.ZkClientUtil;

/**
 * 
 * @author Terrence2013
 * 2018年1月24日 上午9:54:28
 * JSON格式导入配置初始化对象
 */
public class JsonProcessor extends AbsConfigFileProcessor {
    /**
     * json格式导入配置进行初始化
     *
     * @param in
     * @param zkClient
     * @throws IOException
     * @throws UtilException
     */
    @Override
    public void importConfig(InputStream in, ZkClient zkClient) throws IOException, UtilException {
        String json = this.getJsonString(in);
        Iterator<Object> systems = FastJSONUtil.toJSONArray(json).iterator();
        //系统层
        while (systems.hasNext()) {
            JSONObject system = (JSONObject) systems.next();
            if (system.size() > 0) {
                Iterator<Map.Entry<String, Object>> systemEntries = system.entrySet().iterator();
                while (systemEntries.hasNext()) {
                    Map.Entry<String, Object> systemEntry = systemEntries.next();
                    String systemName = systemEntry.getKey();
                    Iterator<Object> modules = ((JSONArray) systemEntry.getValue()).iterator();
                    //模块层
                    while (modules.hasNext()) {
                        JSONObject module = (JSONObject) modules.next();
                        if (module.size() > 0) {
                            Iterator<Map.Entry<String, Object>> moduleEntries = module.entrySet().iterator();
                            while (moduleEntries.hasNext()) {
                                Map.Entry<String, Object> moduleEntry = moduleEntries.next();
                                String moduleName = moduleEntry.getKey();
                                Iterator<Object> configs = ((JSONArray) moduleEntry.getValue()).iterator();
                                //配置层
                                while (configs.hasNext()) {
                                    JSONObject config = (JSONObject) configs.next();
                                    if (config.size() > 0) {
                                        String storeValue = FastJSONUtil.toJSON(new ConfigStoreValue(
                                                config.getBoolean("canEdit"),
                                                config.getBoolean("canDel"),
                                                config.getString("value")));
                                        String path = ZkClientUtil.getNodePath(systemName, moduleName, config.getString("name"));
                                        ZkClientUtil.checkPath(path);
                                        if (!zkClient.exists(path)) {
                                            zkClient.createPersistent(path, true);
                                        }
                                        zkClient.writeData(path, storeValue);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 从流中获取json字符串
     *
     * @param in
     * @return
     * @throws IOException
     */
    private String getJsonString(InputStream in) throws IOException {
        int i = -1;
        byte[] b = new byte[1024];
        StringBuffer sb = new StringBuffer();
        while ((i = in.read(b)) != -1) {
            sb.append(new String(b, 0, i));
        }
        return sb.toString();
    }
}
